<?php

/* ----------------------------------------------
  Caloris: Administration Homepage

  $Id: index.php 75 2011-05-22 06:34:23Z admin $
  $Author: admin $

  Administration module homepage
  ---------------------------------------------- */

require_once("../../../index.php");
Caloris\ACL::authenticate();

define('_MODULE_ACTIVE', 'administrator');

$directoryScan = scandir(_CALSET_PATHS_BASE . _CALSET_DIRS_CACHE);

if ($_REQUEST["function_name"] == 'cache_delete') {
    for ($a = 0; $a < count($directoryScan); $a++) {
        if ($directoryScan[$a] === '.' || $directoryScan[$a] === '..') {
            continue;
        } else {
            unlink(_CALSET_PATHS_BASE . _CALSET_DIRS_CACHE . '/' . $directoryScan[$a]);
        }
    }

    header("location: " . $_SERVER["HTTP_REFERER"] . "");
    exit();
} elseif ($_REQUEST["function_name"] == 'userlist') {
    // Userlist: list of users and their user state

    $xma = simplexml_load_file(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/users.xml');

    if (count($xma->user->children()) > 0) {
        $content .= '';
        for ($a = 0; $a < count($xma->user->children()); $a++) {
            $date_end = Caloris\Time::dateDifference("YYYY-m-d h:n:s", $xma->user[$a]->date->visited, date("Y-m-d H:i:s"));

            if (strcmp($xma->user[$a]["id"], $_COOKIE["auser"]) == 0) {
                $style = 'font-weight: 700;';
            } else {
                $style = 'font-weight: 300;';
            }

            $uriDetail = _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/usermanager/users.detail.php?idf=' . $xma->user[$a]["id"];

            if ($date_end <= 300) {
                $flag = '<img src="' . _CALSET_PATHS_URI . _CALSET_DIRS_TEMPLATE . '/caloris-admin/images/flag_green.gif" alt="' . t('online', 'administrator') . '" title="Online (naposledy v ' . $xma->user[$a]->date->visited . ')" />';
                $content .= '<span style="' . $style . '">' . $flag . ' <a href="' . $uriDetail . '">' . $xma->user[$a]["id"] . '</a></span> ';
            } elseif ($date_end <= 900) {
                $flag = '<img src="' . _CALSET_PATHS_URI . _CALSET_DIRS_TEMPLATE . '/caloris-admin/images/flag_orange.gif" alt="' . t('idle', 'administrator') . '" title="Nečinný (naposledy v ' . $xma->user[$a]->date->visited . ' " />';
                $content .= '<span style="' . $style . '">' . $flag . ' <a href="' . $uriDetail . '">' . $xma->user[$a]["id"] . '</a></span> ';
            } else {
                $flag = '';
                $content .= '';
            }
        }
    }

    echo "{\"userlist\": \"" . addslashes($content) . "\"}";
    exit();
}

$content .= '
<h1>Vítejte</h1>
[cal:admin_toolbar /]';

$content .= PHP_EOL . menu_navigator(4, 'atnav');

$xmlModules = new Caloris\XML();
$xmlModules->source(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/modules.xml');
$xmlModules->filter('//module[hasadmin="1"]');

$content .= '<div id="moduleBox">';

for ($x = 0; $x < count($xmlModules->output); $x++) {
    if (file_exists(_CALSET_PATHS_BASE . '/cal-components/data/module-' . $xmlModules->output[$x]["id"] . '.xml')) {
        $xmlModuleInfoF = simplexml_load_file(_CALSET_PATHS_BASE . '/cal-components/data/module-' . $xmlModules->output[$x]["id"] . '.xml');
        $xmlModuleInfo = $xmlModuleInfoF->xpath('/core');
    }

    if (file_exists(_CALSET_PATHS_BASE . _CALSET_DIRS_MODULE . '/' . $xmlModules->output[$x]["id"] . '/mnu-icon.png')) {
        $iconUrl = _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/' . $xmlModules->output[$x]["id"] . '/mnu-icon.png';
    } else {
        $iconUrl = _CALSET_PATHS_URI . _CALSET_DIRS_TEMPLATE . '/caloris-admin/images/mnu-pages.png';
    }
    
    if (file_exists(_CALSET_PATHS_BASE . _CALSET_DIRS_MODULE . '/' . $xmlModules->output[$x]["id"] . '/index.php')) {
        $moduleURL = _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/' . $xmlModules->output[$x]["id"] . '/index.php';
    } elseif (file_exists(_CALSET_PATHS_BASE . _CALSET_DIRS_MODULE . '/' . $xmlModules->output[$x]["id"] . '/index.html')) {
        $moduleURL = _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/' . $xmlModules->output[$x]["id"] . '/index.html';
    } else {
        $moduleURL = _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/store/index.html?module=' . $xmlModules->output[$x]["id"];
    }

    $content .= '
<div class="module span4">
<div class="moduleIcon"><a href="' . $moduleURL .'">
<img src="' . $iconUrl . '" alt="' . $xmlModules->output[$x]->name . '" /></a></div>
<div class="moduleText">
<p class="moduleHeading"><a href="' . $moduleURL .'">' . $xmlModules->output[$x]->name . '</a></p>
<p class="moduleDescription">' . $xmlModules->output[$x]->description . '</p>
    </div>
</div>';
}

$content .= '
</div>

<h2>Přihlášení uživatelé</h2>

<div class="clearl"></div>

<script type="text/javascript">
$.getJSON("[cal:def(paths;uri) /][cal:def(dirs;admin) /]/index.php?function_name=userlist", function(resp){
	$("#userlist").html(resp.userlist);
	});
</script>
<div id="userlist"></div>
<div>
</div>';

$data["title"] = t('homepage', 'administrator');

$tmpl = new Caloris\Template;
$tmpl->input($content);
$tmpl->renderHead($data);
echo $tmpl->renderBody('caloris-admin');